<?php
	/*
	 * All rights Reserved, Designed By www.youyacao.com <br>
	 * @Description:类型 <br>
	 * @version 蜻蜓C影视追剧系统  <br>
	 * @author:成都市一颗优雅草科技有限公司  <br>
	 * 注意：使用我司开源源代码请遵循license文件的协议仅供个人非盈利使用，禁止用于其他的商业用途
	 * 需要商业用途或者定制开发等可访问songshu.youyacao.com  联系QQ:2853810243 422108995 23625059584
	 * 正版系统查询系统 zhengbancha.youyacao.com
	 */


namespace App\Admin\Controllers;

use App\Admin\Repositories\Type;
use App\Models\Tag;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TypeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Type(['identifys']), function (Grid $grid) {
            $grid->id('ID')->bold()->sortable();
            $grid->title->tree(); // 开启树状表格功能
            $grid->ico->image();
            $grid->column('order')->editable();
            $grid->column('identifys.name','标签');
            $grid->column('is_xian', '是否显示')->switch();
            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->quickSearch(['id', 'title']);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Type(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('ico');
            $show->field('parent_id');
            $show->field('order');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Type(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->image('ico');
            $form->radio('is_xian', '是否显示')->options([0 => '不显示', 1 => '显示'])->default(1);
//            $form->text('parent_id');
            $form->tree('parent_id', '归属')
                ->setTitleColumn('title')
                ->treeState(false)
                ->nodes(\App\Models\Type::get()->toArray())
                ->saving(function ($v) {
                    // 转为 , 隔开的字符串
                    if(!empty($v)){
                        return implode(',', $v);
                    }else{
                        return 0;
                    }
                });
            $form->number('order')->default(0);
            $form->select('identify','标签')->options(Tag::pluck('name','identify'));

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
